// pages/posts-detail/posts-detail.js
import {
    postList
} from "../../data/data.js";
const app = getApp();
// console.log(app);
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShou: false,
        isPlaying: false,
        data: {},
        _pid: null,
        _postCollectend: {},
        _mgr: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const postID = options['postId'];
        let stro = null;
        let nowShou = null;
        try {
            //把await及获取它的值的操作放在try里
            stro = await wx.getStorage({
                key: 'isS',
            });
            this.data._postCollectend = stro['data'];
            nowShou = stro['data'][postID];
            // console.log(33);
        } catch (err) {
            //失败的操作放在catch里
            console.log('输了' + err)
        }

        let onePostData = null;
        for (let i = 0; i < postList.length; i++) {
            if (postList[i]['postId'] == postID) {
                onePostData = postList[i];
            }
        }

        const mgr = wx.getBackgroundAudioManager();
        mgr.onPlay(this.onMusic);
        mgr.onPause(this.onMusicStop);
        // mgr.onStop();

        this.data._mgr = mgr;
        this.data._pid = postID;
        this.setData({
            "data": onePostData,
            "isShou": nowShou,
            // "isPlaying": app.gIsPlayingMusic
            "isPlaying": this.currentPostIsPlaying()
        });
    },

    currentPostIsPlaying() {
        if (app.gIsPlayingMusic && app.gIsPlayingPostId == this.data._pid) {
            return true;
        }
        return false;
    },

    async shouCang(option) {
        var obj_data = this.data._postCollectend;

        obj_data[this.data._pid] = !this.data.isShou;

        this.setData({
            isShou: !this.data.isShou
        });
        wx.lin.showMessage({
            type: this.data.isShou ? 'success' : 'warning',
            icon: this.data.isShou ? 'success' : 'warning',
            content: this.data.isShou ? '收藏成功' : "已取消收藏"
        })

        // 写入缓存
        wx.setStorage({
            key: "isS",
            data: obj_data
        });
    },

    fenXiang(option) {
        console.log(66);
        wx.lin.showActionSheet({
            itemList: [{
                    name: '今日不再出现此类内容'
                },
                {
                    name: '屏蔽'
                }
            ]
        })
    },

    onMusic(option) {
        const mgr = this.data._mgr;
        let music = this.data.data.music;
        mgr.src = music.url;
        mgr.title = music.title;
        mgr.coverImgUrl = music.coverImg;

        app.gIsPlayingMusic = true;
        app.gIsPlayingPostId = this.data.data.postId;
        this.setData({
            isPlaying: true
        });
    },

    onMusicStop(event) {
        const mgr = this.data._mgr;
        mgr.stop();
        app.gIsPlayingMusic = false;
        app.gIsPlayingPostId = -1;
        this.setData({
            isPlaying: false
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})